package com.mtcarpenter.nashorn;

import org.junit.Test;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

/**
 * @Author: mtcarpenter
 * @describe
 * @Date: 2019/10/8
 * @Version 1.0
 */
public class NashornScriptTest {

    /**
     * Nashorn  运行 JavaScript
     */
    @Test
    public void test01(){
        ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
        ScriptEngine nashorn = scriptEngineManager.getEngineByName("nashorn");
        String name = "Java 8 新特性必知必会";

        try {
            nashorn.eval("print('" + name + "')");
            Integer result = (Integer)nashorn.eval("5 + 20/2 ");
            System.out.println(result);
        } catch (ScriptException e) {
            System.out.println("script 执行出错 : " + e.getMessage());
        }
    }

}
